﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using DelonixRegia__Staff_Use_.Classes;

namespace DelonixRegia__Staff_Use_.Classes
{

    public class StaffLogin
    {
        private string username;
        private string password;
        private string accounttype;
private   string p1;
private   string p2;
    public StaffLogin()
        {

        }
        public StaffLogin(string username, string password, string accounttype)
        {
            this.username = username;
            this.password = password;
            this.accounttype = accounttype;
        }

public    StaffLogin(string p1,string p2)
    {
        // TODO: Complete member initialization
this.p1 = p1;
this.p2 = p2;
    }
        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        public string Accounttype
        {
            get{return accounttype;}
            set{accounttype = value;}
        }

        public static int CreateLogin(StaffLogin u)
        {
            int rowsinserted = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
               conn.ConnectionString=" Data source =.;Initial Catalog = Delonix; Integrated Security = true";
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO StaffLogin(username,password,accounttype)"
                    + " VALUES(@username, @password,@accounttype)";
                comm.Parameters.AddWithValue("@username", u.username);
                comm.Parameters.AddWithValue("@password", u.password);
                comm.Parameters.AddWithValue("@accounttype", u.accounttype);
                rowsinserted = comm.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            conn.Close();
            return rowsinserted;
        }
        //public  string GetUsername(string username)
        //{
        //    SqlConnection conn = null;
        //    try
        //    {
        //        conn = new SqlConnection();
        //        conn.ConnectionString = " data source =localhost;initial catalog = Delonix; integrated security = true";
        //        conn.Open();
        //        SqlCommand comm = new SqlCommand();
        //        comm.Connection = conn;
        //        comm.CommandText = "SELECT username@username FROM StaffLogin WHERE username=@username";
        //        comm.Parameters.AddWithValue("@username", username);
        //        SqlDataReader dr = comm.ExecuteReader();
        //    }
        
        //    catch (SqlException e)
        //    {
        //        throw e;
        //    }
        //}
        public static bool Login(string username, string password)
        {
            bool success = false;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = " Data source =localhost;Initial Catalog = Delonix; Integrated Security = true";
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM StaffLogin WHERE username=@username and password=@password";
                comm.Parameters.AddWithValue("@username", username);
                comm.Parameters.AddWithValue("@password", password);
                SqlDataReader dr = comm.ExecuteReader();
                if (dr.Read())
                {
                    success = true;
                }
                else
                {
                    success = false;
                }
            }
            catch (SqlException e)
            {
                throw e;
            }
            conn.Close();
            return success;
        }


        public static int DeleteAccount(string Username)
        {
            int rowsdeleted = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = " Data source =localhost;Initial Catalog = Delonix; Integrated Security = true";
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "DELETE FROM LoginManagement WHERE username=@username";
                comm.Parameters.AddWithValue("@username", Username);
                rowsdeleted = comm.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            conn.Close();
            return rowsdeleted;
        }
        public static StaffLogin GetAccounttypeByUsername(string username)
        {
            StaffLogin staff = new StaffLogin();
            SqlConnection conn = null;
          try
    {
        conn = new SqlConnection();
        conn.ConnectionString = " Data source =localhost;Initial Catalog = Delonix; Integrated Security = true";
        conn.Open();
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;

        comm.CommandText = "SELECT accounttype FROM StaffLogin WHERE username=@username";
        comm.Parameters.AddWithValue("@username", username);
        SqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {
            staff.accounttype = (string)dr["accounttype"];

        }
        dr.Close();
    }
    catch (SqlException e)
    {
        throw e;
    }
    conn.Close();
    return staff;

            //OK LIAO GO TRY
}


    

}
    }
